<html>
<head><meta charset="utf-8"><title>rename functions · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html">rename functions</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="159956032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159956032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jean Mertz <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159956032">(Mar 04 2019 at 22:12)</a>:</h4>
<p>I was wondering if there's a reason why function renaming isn't supported yet? Is it just a matter of priority? If so, I was looking into what it'd take to implement it, and one of the things I noticed is that <a href="https://github.com/rust-analyzer/rust-analyzer/blob/bbaf750b10810c21d42710a5d12181ca73099525/crates/ra_ide_api/src/references.rs#L87" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/bbaf750b10810c21d42710a5d12181ca73099525/crates/ra_ide_api/src/references.rs#L87"><code>find_node_at_offset</code> returns <code>None</code></a> if I add a test for function renaming. Looking at the AST, the function name is of type <code>NAME</code>, and that check expects <code>NAME_REF</code>. Any pointers on what to do to make this work?</p>



<a name="159979461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159979461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159979461">(Mar 05 2019 at 06:00)</a>:</h4>
<p>I think one problem is that we don't currently handle project wide references, including functions. Meaning you cannot use find all references on a function currently, which is used when renaming non-modules.</p>



<a name="159980982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159980982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159980982">(Mar 05 2019 at 06:45)</a>:</h4>
<p>Tracking issue for project wide references can be found in <a href="https://github.com/rust-analyzer/rust-analyzer/issues/200" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/issues/200">rust-analyzer#200</a></p>



<a name="159983020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159983020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jean Mertz <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159983020">(Mar 05 2019 at 07:38)</a>:</h4>
<p>Thanks for the pointer. Yeah, that seems like a big one to tackle.</p>



<a name="159983048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159983048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jean Mertz <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159983048">(Mar 05 2019 at 07:39)</a>:</h4>
<p>Still though, I'm curious if you have any feedback on my question of NAME_REF vs NAME, what their differences are, and why the function signature returns NAME (which incidentally doesn't match in the link I gave above)?</p>



<a name="159983783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159983783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159983783">(Mar 05 2019 at 07:56)</a>:</h4>
<p><span class="user-mention" data-user-id="124920">@Jean Mertz</span> there should be two cases for renaming:</p>
<p>renaming the declaration (where we do find references and then renaming)</p>
<p>renaming the reference (where we first need to to goto definition, and <em>than</em> find usages&amp;rename)</p>



<a name="159983804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159983804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159983804">(Mar 05 2019 at 07:57)</a>:</h4>
<p>I think that bit of code doesn't handle the first case, while it should</p>



<a name="159983811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159983811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159983811">(Mar 05 2019 at 07:57)</a>:</h4>
<p>The test case would be something like this:</p>
<div class="codehilite"><pre><span></span>fn main() {
    let foo&lt;|&gt; = 92;
    foo + foo;
}
</pre></div>



<a name="159989190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159989190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jean Mertz <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159989190">(Mar 05 2019 at 09:40)</a>:</h4>
<p>Ah. That makes sense. Thanks. I’ll look at this after work.</p>



<a name="159990121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159990121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159990121">(Mar 05 2019 at 09:55)</a>:</h4>
<p>I think you may be able to use <code>find_node_at_offset::&lt;ast::BindPat&gt;</code> directly in the non-<code>NameRef</code> case.  and combining it with <code>source_binder::function_from_child_node</code></p>



<a name="159990375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159990375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159990375">(Mar 05 2019 at 10:00)</a>:</h4>
<p>Or perhaps you first have to check that the node is <code>Name</code> before getting the bind pattern</p>



<a name="159997042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/159997042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jean Mertz <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#159997042">(Mar 05 2019 at 12:05)</a>:</h4>
<p>I just took a quick peek, and that's already what's happening here. And indeed, I added a test and it passes, so this already works, it just doesn't work for functions yet (which is then – I suspect – related to project wide references not being a thing yet).</p>



<a name="160008374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/160008374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#160008374">(Mar 05 2019 at 14:45)</a>:</h4>
<p>Yep it's because of project wide references. Note that renaming is broken into two steps at the LSP layer: a prepare step where we should do validation (and we currently don't  really do any) and the actual rename operation</p>



<a name="160012406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/160012406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> vipentti <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#160012406">(Mar 05 2019 at 15:32)</a>:</h4>
<p>What kind of validation should be done?</p>



<a name="160094351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rename%20functions/near/160094351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rename.20functions.html#160094351">(Mar 06 2019 at 13:22)</a>:</h4>
<p>probably things like: is the input blank, is it a valid identifier, can we actually rename the thing we are on etc.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>